﻿// Copyright (c) Microsoft Corporation 2005-2012.
// This sample code is provided "as is" without warranty of any kind. 
// We disclaim all warranties, either express or implied, including the 
// warranties of merchantability and fitness for a particular purpose. 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.IntegrationFramework.Transform;

namespace FMInc.Transforms
{
    public class CsvToXmlTransform : ITransform
    {
        public void Transform(System.IO.Stream input, System.IO.Stream output, string configuration)
        {
            var reader = new StreamReader(input);
            var outputString = new StringBuilder();

            // Header XML Data
            outputString.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
            outputString.AppendLine("<Envelope xmlns=\"http://schemas.microsoft.com/dynamics/2011/01/documents/Message\">");
            outputString.AppendLine("<Header><Action>http://tempuri.org/FMIncCarDocumentService/create</Action></Header>");
            outputString.AppendLine("<Body><MessageParts>");
            outputString.AppendLine("<FMIncCarDocument xmlns=\"http://schemas.microsoft.com/dynamics/2008/01/documents/FMIncCarDocument\">");

            // Fill Body XML Data
            while (!reader.EndOfStream)
            {
                outputString.AppendLine(DoTransform(reader.ReadLine()));
            }

            // Footer XML Data
            outputString.AppendLine("</FMIncCarDocument></MessageParts></Body></Envelope>");


            WriteTextToStream(output, outputString.ToString());
        }


        // This function takes a single line of a CSV as input,
        // then returns that line converted to a XML format.
        public static string DoTransform(string input)
        {
            string[] values;
            values = input.Split(",".ToCharArray());
       
            StringBuilder output = new StringBuilder();

            output.AppendLine("<FMIncCars_1 class=\"entity\">");

            output.Append("<VIN>");
            output.Append(values[0]);
            output.AppendLine("</VIN>");

            output.Append("<Make>");
            
            output.Append(values[1]);
            output.AppendLine("</Make>");
            
            output.Append("<Model>");
            output.Append(values[2]);
            output.AppendLine("</Model>");

            output.Append("<Mileage>");
            output.Append(values[3]);
            output.AppendLine("</Mileage>");

            output.Append("<Color>");
            output.Append(values[4]);
            output.AppendLine("</Color>");

            output.Append("<Status>");
            output.Append(values[5]);
            output.AppendLine("</Status>");

            output.Append("<Year>");
            output.Append(values[6]);
            output.AppendLine("</Year>");

            output.AppendLine("</FMIncCars_1>");

            return output.ToString();
        }


        public static void WriteTextToStream(Stream stream, string text)
        {
            using (var streamWriter = new StreamWriter(stream))
            {
                streamWriter.Write(text);
            }
        }
    }
}































